package com.whmskj.xjlhsc.mobie.sctz;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.domain.CorpMember;
import com.whmskj.xjlhbc.domain.PersonnelManagementDataSheets;
import com.whmskj.xjlhbc.mapper.CorpMemberMapper;
import com.whmskj.xjlhbc.service.IPersonnelManagementDataSheetsService;
import com.whmskj.xjlhsc.entity.Sc04sctz;
import com.whmskj.xjlhsc.entity.Sc04sctzjl;
import com.whmskj.xjlhsc.mapper.Sc04sctzMapper;
import com.whmskj.xjlhsc.mapper.Sc04sctzjlMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 生产通知
 */
@RestController("scSctzController")
@Tag(name = "生产通知",description = "生产通知")
@RequestMapping("/sc/sctz")
public class SctzController {

    @Autowired
    private Sc04sctzMapper sctzMapper;

    @Autowired
    private Sc04sctzjlMapper sctzjlMapper;

    @Autowired
    private IPersonnelManagementDataSheetsService personnelManagementDataSheetsService;
    @Autowired
    private CorpMemberMapper corpMemberMapper;

    /**
     * 生产通知列表
     */
    @GetMapping("/tzList")
    @Operation(description = "生产通知列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult tzList(){
        Page.startPage();
        LambdaQueryWrapper<Sc04sctz> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Sc04sctz::getReleaseTime);
        List<Sc04sctz> list = sctzMapper.selectList(wrapper);
        list.forEach(item-> item.setPublisher(getCorpMemberName(item.getPublisher())));
        return Page.getDataTable(list);
    }

    /**
     * 首页生产通知
     */
    @GetMapping("/index")
    @Operation(description = "首页生产通知")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult index(){
        LambdaQueryWrapper<Sc04sctz> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Sc04sctz::getReleaseTime).last("limit 1");
        Sc04sctz result = sctzMapper.selectOne(wrapper);
        result.setPublisher(getCorpMemberName(result.getPublisher()));
        return AjaxResult.success(result);
    }

    private String getCorpMemberName(String memberId){
        LambdaQueryWrapper<CorpMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CorpMember::getMemberId, memberId).last("limit 1");
        CorpMember member = Optional.ofNullable(corpMemberMapper.selectOne(wrapper)).orElseGet(CorpMember::new);
        return member.getNick();
    }

    @GetMapping("/tzDetail/{id}")
    @Operation(description = "生产通知详情")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult tzDetail(@PathVariable Long id){
        Sc04sctz sctz = sctzMapper.selectById(id);
        sctz.setPublisher(getCorpMemberName(sctz.getPublisher()));
        Sc04sctzjl sctzjl = new Sc04sctzjl();
        PersonnelManagementDataSheets personnelManagementDataSheets = personnelManagementDataSheetsService.getByLoginUserId();
        String fe1e = personnelManagementDataSheets.getFe1e();
        LambdaQueryWrapper<Sc04sctzjl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sc04sctzjl::getFa81,id);
        wrapper.eq(Sc04sctzjl::getAccess1, fe1e);
        Long count = sctzjlMapper.selectCount(wrapper);
        if(Objects.isNull(count) || count==0) {
            sctzjl.setAccess1(fe1e);
            sctzjl.setPositions(personnelManagementDataSheets.getPositions1());
            sctzjl.setDateTime(DateUtil.now());
            sctzjl.setFa81(id);
            sctzjlMapper.insert(sctzjl);
        }
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sc04sctzjl::getFa81,id);
        wrapper.orderByDesc(Sc04sctzjl::getDateTime);
        List<Sc04sctzjl> cyjlList = sctzjlMapper.selectList(wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("tzDetail", sctz);
        map.put("cyjlList", cyjlList);
        return AjaxResult.success(map);
    }


}
